<?php

class Rate extends database_object{
    public $id;
    public $connection_id;
    public $user_id;
    public $value;
    public $timestamp;
  
    
    protected static $table_name = "bo_rate";
    protected static $db_fields = array("id", "value", "user_id","connection_id","timestamp");
    
    
    // Method that builds a rate object
   public static function build($connection_id=0,$value=0,$user_id=0){
                
                if($connection_id !=0 || $rate!=0 || $user_id!=0){
                    
                $rate = new Rate();
                $rate->connection_id = $connection_id;
                $rate->value    = $value;
                $rate->user_id = $user_id;
                $rate->save();
                
                return true;
                }else{
                    false;
                }
   }
    
    

    //Method finds the average rate by age ($min to $max) and sex, depending on parameters, on a specific con_id
    public function getAvgRateByAge($min=0, $max=0, $con_id=0, $sex){
        
        $database = Database::getInstance();
        $sql = "SELECT AVG(value) as avgrate
                FROM bo_user, bo_rate, bo_company_product
                WHERE (year(CURRENT_TIMESTAMP ) - yearborn) >{$min}
                && year(CURRENT_TIMESTAMP ) - yearborn <{$max}
                AND bo_company_product.id ={$con_id}
                AND bo_user.id = bo_rate.user_id
                AND bo_company_product.id = bo_rate.connection_id
                AND bo_user.sex = '{$sex}';";
             
        $res =  $database->query($sql);
        
        while ($row = $database->fetch_array($res)) {
                $tab = (float)$row['avgrate'];
            }
        
        return $tab;
    }
    
    // Method finds the amount of users within an age range from $max to $min based on $sex
    public function getCountBySexAge($min=0, $max=0, $con_id=0, $sex){
         $database = Database::getInstance();
        $sql = "SELECT COUNT(value) as countnr
                FROM bo_user, bo_rate, bo_company_product
                WHERE (year(CURRENT_TIMESTAMP ) - yearborn) >{$min}
                && year(CURRENT_TIMESTAMP ) - yearborn <{$max}
                AND bo_company_product.id ={$con_id}
                AND bo_user.id = bo_rate.user_id
                AND bo_company_product.id = bo_rate.connection_id
                AND bo_user.sex = '{$sex}';";
             
        $res =  $database->query($sql);
        
        while ($row = $database->fetch_array($res)) {
                $tab = (float)$row['countnr'];
            }
            return $tab;
        
    }
    
    //method returns the averagerate on a connection_id
     public static function getAvgRate($id){
        $database   = Database::getInstance();
        $sql        = "SELECT AVG(value) as avgrate from bo_rate where connection_id = {$id}";
        $res        =  $database->query($sql);
        
        if($row = $database->fetch_array($res)){
            
             $tab = (float)$row['avgrate'];
        }
        
        return $tab;
    }
    }
    $rate = new Rate();
?>

